package com.hdyg.sixg.activity.mine;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.MineBean;
import com.hdyg.sixg.mvp.Contrant.CUpdatePwd;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PUpdatePwd;
import com.hdyg.sixg.util.CountDownTimerUtil;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import butterknife.BindView;

/**
 * @author CZB
 * @describe 修改密码
 * @time 2019/4/8 16:45
 */
public class UpdatePwdActivity extends BaseActivity implements CUpdatePwd.IVUpdatePwd {

    private final String UPDATE_LOGIN_PWD = "1";   //修改登录密码
    private final String UPDATE_TRANS_PWD = "2";   //修改交易密码

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_trans)
    TextView tvTrans;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_pwd_2)
    EditText etPwd2;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;

    private String phone;
    private String type = UPDATE_LOGIN_PWD;
    private CUpdatePwd.IPUpdatePwd mPresenter;
    private String new_quhao,code, new_password, confirm_password;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_update_pwd;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.mine_pwd);
        phone = getIntent().getStringExtra(SpMsg.USER_PHONE);
        tvPhone.setText(StringUtil.settingPhone(phone));
        setChangeUI(true);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(1000);//传入false表示刷新失败
        });
        llTopLeft.setOnClickListener(v -> finish());
        //登录密码
        tvLogin.setOnClickListener(v -> {
            setChangeUI(true);
        });
        //交易密码
        tvTrans.setOnClickListener(v -> {
            setChangeUI(false);
        });
        //获取验证码
        tvCode.setOnClickListener(v -> {
            if (TextUtils.isEmpty(phone)) {
                toastNotifyShort(R.string.regist_phone_hint);
                return;
            }
            if (TextUtils.isEmpty(new_quhao)) {
                return;
            }
            if (type.equals(UPDATE_LOGIN_PWD) ) {
                //修改登录密码发送短信
                mPresenter.pGetCode(BaseUrl.SEND_CODE_URL, GetParamUtil.getSendCodeParam(new_quhao, phone, BaseUrl.CODE_TYPE_UPDATE_LOGIN_PWD));
            } else {
                //修改交易密码发送短信
                mPresenter.pGetCode(BaseUrl.SEND_CODE_URL, GetParamUtil.getSendCodeParam(new_quhao, phone, BaseUrl.CODE_TYPE_UPDATE_TRANS_PWD));
            }
        });
        //确认修改
        btSure.setOnClickListener(v -> {
            code = etCode.getText().toString().trim();
            new_password = etPwd.getText().toString().trim();
            confirm_password = etPwd2.getText().toString().trim();
            if (TextUtils.isEmpty(code)) {
                toastNotifyShort(R.string.regist_code_hint);
                return;
            }
            if (TextUtils.isEmpty(new_password)) {
                toastNotifyShort(R.string.login_pwd_hint);
                return;
            }
            if (!new_password.equals(confirm_password)) {
                if (type.equals(UPDATE_LOGIN_PWD)) {
                    toastNotifyShort(R.string.toast_login_pwd);
                } else {
                    toastNotifyShort(R.string.toast_trans_pwd);
                }
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetSureUpdate(BaseUrl.CHANGE_PWD_URL, GetParamUtil.getChangePwdParam(code, type, new_password, confirm_password));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.PHONE_URL, GetParamUtil.getPhoneParam());
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PUpdatePwd(this);
    }

    /**
     * 改变UI颜色
     *
     * @param flag true登录密码  false交易密码
     */
    private void setChangeUI(boolean flag) {
        if (flag) {
            tvLogin.setTextColor(getResources().getColor(R.color.main_color));
            tvTrans.setTextColor(getResources().getColor(R.color.gray_80));
            type = UPDATE_LOGIN_PWD;
        } else {
            tvLogin.setTextColor(getResources().getColor(R.color.gray_80));
            tvTrans.setTextColor(getResources().getColor(R.color.main_color));
            type = UPDATE_TRANS_PWD;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CountDownTimerUtil.getInstance().stopTimer();
    }

    @Override
    public void vGetInitSuccess(MineBean dataBean) {
        new_quhao = dataBean.getQuhao();
    }

    @Override
    public void vGetCodeSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        CountDownTimerUtil.getInstance().startTimer(tvCode, 60);
    }

    @Override
    public void vGetSureUpdateSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        finish();
    }
}
